package com.xl.utils.excel.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xl.exception.GlobalException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:XueH
 * @Description:
 * @Date:Created in 16:41 2023/6/5
 * @Modified By:
 */
public class ExcelReadListenerConfig extends AnalysisEventListener<Map<Integer, String>> {
    /**
     * 表头数据（存储所有的表头数据）
     */
    private final List<Map<Integer, String>> headList = new ArrayList<>();
    /**
     * 数据体
     */
    private final List<Map<Integer, String>> dataList = new ArrayList<>();


    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        headList.add(headMap);
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext analysisContext) {
        if (data.size() == 1000) {
            throw new GlobalException("Excel导入行数不得超过1000行");
        }
        dataList.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    public List<Map<Integer, String>> getHeadList() {
        return headList;
    }

    public List<Map<Integer, String>> getDataList() {
        return dataList;
    }

}
